package com.drg.center.dao;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.drg.center.entity.DictOperationPO;
import com.drg.center.mapper.DictOperationMapper;
import com.guaike.core.mybatis.plugins.BaseDAO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public class DictOperationDAO extends BaseDAO<DictOperationMapper, DictOperationPO> {

    public List<DictOperationPO> searchOperation(String search) {
        LambdaQueryWrapper<DictOperationPO> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(search)) {
            queryWrapper.like(DictOperationPO::getOperationCode, search)
                    .or().like(DictOperationPO::getOperationName, search)
                    .or().like(DictOperationPO::getMedicareOperationCode, search)
                    .or().like(DictOperationPO::getMedicareOperationName, search);
        }
        queryWrapper.last("limit 0,20");
        return getBaseMapper().selectList(queryWrapper);
    }

    public List<DictOperationPO> searchHospitalOperation(String search) {
        LambdaQueryWrapper<DictOperationPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNull(DictOperationPO::getMedicareOperationCode);
        if (StringUtils.isNotBlank(search)) {
            queryWrapper.and(wrapper -> wrapper.like(DictOperationPO::getOperationCode, search)
                    .or().like(DictOperationPO::getOperationCode, search));
        }
        queryWrapper.last("limit 0,20");
        return getBaseMapper().selectList(queryWrapper);
    }

    public List<DictOperationPO> searchMatchOperation() {
        LambdaQueryWrapper<DictOperationPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNotNull(DictOperationPO::getMedicareOperationCode);
        return getBaseMapper().selectList(queryWrapper);
    }

    public List<DictOperationPO> searchMatchOperation(String search) {
        LambdaQueryWrapper<DictOperationPO> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.isNotNull(DictOperationPO::getMedicareOperationCode).isNotNull(DictOperationPO::getOperationCode);
        if (StringUtils.isNotBlank(search)) {
            queryWrapper.and(wrapper -> wrapper.like(DictOperationPO::getOperationCode, search)
                    .or().like(DictOperationPO::getOperationName, search)
                    .or().like(DictOperationPO::getMedicareOperationCode, search)
                    .or().like(DictOperationPO::getMedicareOperationName, search));
        }
        queryWrapper.last("limit 0,20");
        return getBaseMapper().selectList(queryWrapper);
    }

    public int cancelMatchOperation(List<Long> ids) {
        LambdaUpdateWrapper<DictOperationPO> updateWrapper = new LambdaUpdateWrapper<>();
        updateWrapper.set(DictOperationPO::getMedicareOperationCode, null);
        updateWrapper.set(DictOperationPO::getMedicareOperationName, null);
        updateWrapper.in(DictOperationPO::getId, ids);
        return getBaseMapper().update(new DictOperationPO(), updateWrapper);
    }
}
